package com.swt.draw.controller;

import com.swt.draw.entity.Admin;
import com.swt.draw.entity.YjTemplate;
import com.swt.draw.service.TemplateService;
import com.swt.draw.vo.BaseResponse;
import com.swt.draw.vo.TemplateVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/27 0027 23:23
 * @Version 1.0
 **/
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Resource
    private TemplateService templateService;

    /**
     * @Author ZPF
     * @Description 分页获取模板信息
     * @Date 0:07 2019/10/28 0028
     * @Param [start, end]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @RequestMapping("/getTemplatesAsPage")
    public BaseResponse getTempaltes(Integer start, Integer end){
        BaseResponse response = new BaseResponse();
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        List<YjTemplate> templates = templateService.getYjTemplates(admin.getAdminNo(), start, end);
        response.setCode(1001);
        response.setDesc("success");
        response.setData(templates);
        return response;
    }

    /**
     * @Author ZPF
     * @Description 获取模板的元件集合
     * @Date 1:41 2019/10/28 0028
     * @Param [jh, time]
     * @return com.swt.draw.vo.BaseResponse<java.util.List<com.swt.draw.vo.TemplateVO>>
     **/
    @RequestMapping("/getYjs")
    public BaseResponse<List<TemplateVO>> getTemplateYjs(String jh, String time){
        BaseResponse<List<TemplateVO>> response = new BaseResponse<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        try {
            Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
            List<TemplateVO> templateVOS = templateService.getTemplates(jh, format.parse(time.replace(" ", "+")),admin.getAdminNo());
            response.setData(templateVOS);
            response.setCode(1001);
            response.setDesc("success");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * @Author ZPF
     * @Description 删除模板
     * @Date 15:48 2019/11/2 0002
     * @Param [jh, time]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/delete")
    public BaseResponse delTemplate(String jh, String time) throws ParseException {
        BaseResponse response=new BaseResponse();
        System.out.println(jh);
        System.out.println(time);
        Integer row = templateService.delTemplateByJhAndTime(jh,time);
        if(row > 0){
            response.setCode(1001);
            response.setDesc("success");
        }
        return response;
    }
}
